
#include "Player.h"
#include "HumanPlayer.h"
#include "Ship.h"
#include "Input/Input.h"
#include "HUD.h"
#include "World.h"

#include <iostream>
#include <cassert>
#include <SDL/SDL.h>
#include <cmath>

HumanPlayer::HumanPlayer(unsigned player_id, std::string player_name, unsigned team_id) :
    Player(player_id, player_name, team_id) {

    spawnNewShip();
}

HumanPlayer::~HumanPlayer(){

}


void HumanPlayer::update(void){
    checkHealth();
    if(amAlive()){
        last_alive_tick = Timer::currentTicks();
    }
    else if(Timer::currentTicks()-last_alive_tick < 2000){
        return;
    }
    else{
        spawnNewShip();
    }


    handleKeyInput();
    followCursor();
}

void HumanPlayer::handleKeyInput(void){
    Ship *pship = (Ship*)(my_ship_ref.getPointer());
    if(pship == NULL){ return; }

    if (Input::instance().isKeyDown("fire_pweapon")) {
        pship->fire(true);
    }
    if (Input::instance().isKeyDown("fire_sweapon")) {
        pship->fire(false);
    }

    if (Input::instance().isKeyDown("move_forward")) {
        pship->accelerate(1.0f, false);
    }
    if (Input::instance().isKeyDown("move_backward")) {
        pship->accelerate(-1.0f, false);
    }
    
    if (Input::instance().isKeyDown("move_left")) {
        pship->side_thrust(-1.0f);
    }
    if (Input::instance().isKeyDown("move_right")) {
        pship->side_thrust(1.0f);
    }
/*
    std::vector<SDLKey> down_keys = Input::instance().getAllDownKeys();
    for(unsigned i = 0; i < down_keys.size(); i++){
        SDLKey key = down_keys[i];
        switch(key){
          case SDLK_w:
            my_ship->accelerate(1.0f);
            break;
          case SDLK_s:
            my_ship->accelerate(-1.0f);
            break;
          case SDLK_SPACE:
            my_ship->fire();
            break;
          default:
            break;
        }
    }
    */
    if (Input::instance().isKeyDown("switch_pweapon_forward")) {
        pship->switch_weapon(true, true);
    }
    if (Input::instance().isKeyDown("switch_pweapon_backward")) {
        pship->switch_weapon(true, false);
    }

    if (Input::instance().isKeyDown("switch_sweapon_forward")) {
        pship->switch_weapon(false, true);
    }
    if (Input::instance().isKeyDown("switch_sweapon_backward")) {
        pship->switch_weapon(false, false);
    }
}

void HumanPlayer::followCursor(void){
    Ship *pship = (Ship*)(my_ship_ref.getPointer());
    if(pship == NULL){ return; }

    // Update the orientation of the ship depending on where the cursor is.
    Vector2D target_pos = HUD::instance().getCursorWorldPosition();
    Vector2D offset = target_pos - pship->getPosition();
    offset.normalise();

    float angle = acosf(offset.dotProduct(pship->getHeading())) * 180.0f/M_PI;
    if(angle != angle){ // We need to check for "nan" here because acos(1.0f) = nan, but should be 0 
        angle = 0.0f;
    }

    offset.rotate(-pship->getRotation());

    if(offset.x < 0.0f){
        pship->turn(angle);
    }
    else{
        pship->turn(-angle);
    }
}

